package com.krtech.wecard.utils.sms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.enums.SysEnums;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 短信发送工具类
 */
@Slf4j
@Component
public class SendSMSUtil {

    @Autowired
    private SysKeyValueService sysKeyValueService;


    /**
     * 密码修改
     * PhoneNumberSet:通知的电话集合
     */
    public void passwordChangeNotify(String[] PhoneNumberSet){
        SysKeyValue infoChangeNotifyId = sysKeyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, SysEnums.TENCENT_SMS_INFO_CHANGE_NOTIFY_ID));
        if (infoChangeNotifyId == null){
            throw new BusinessException("密码变更通知短信模版不存在");
        }

        String[] TemplateParamSet = {""};
        this.sendSMS(PhoneNumberSet,infoChangeNotifyId.getSysValue(),TemplateParamSet);
    }

    /**
     * 信息变更短信通知
     * PhoneNumberSet:通知的电话集合
     */
    public void infoChangeNotify(String[] PhoneNumberSet){
        SysKeyValue infoChangeNotifyId = sysKeyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, SysEnums.TENCENT_SMS_INFO_CHANGE_NOTIFY_ID));
        if (infoChangeNotifyId == null){
            throw new BusinessException("信息变更通知短信模版不存在");
        }

        String[] TemplateParamSet = {""};
        this.sendSMS(PhoneNumberSet,infoChangeNotifyId.getSysValue(),TemplateParamSet);
    }


    /**
     * 短信发送最下层方法
     *
     * @param PhoneNumberSet：发送给电话的人
     * @param templateId：模版id
     * @param TemplateParamSet：模版的参数
     */
    public void sendSMS(String [] PhoneNumberSet,String templateId,String [] TemplateParamSet){

        //获得secretId
        SysKeyValue secretIdKeyvalue = sysKeyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, SysEnums.TECENT_SMS_SECRET_ID));
        String secretId = secretIdKeyvalue.getSysValue();

        //获得secretKey
        SysKeyValue secretKeyKeyvalue = sysKeyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, SysEnums.TECENT_SMS_SECRET_KEY));
        String secretKey = secretKeyKeyvalue.getSysValue();

        //获得appId
        SysKeyValue appIdKeyValue = sysKeyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, SysEnums.TECENT_SMS_APP_ID));
        String appId = appIdKeyValue.getSysValue();

        //获得signName
        SysKeyValue signNameValue = sysKeyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, SysEnums.TENCENT_SMS_SIGN_NAME));
        String signName = signNameValue.getSysValue();

        if (secretId == null || secretKey == null){
            throw new BusinessException("发送短信的SecretId或者SecretKey为空");
        }
        try{
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(PhoneNumberSet);

            req.setSmsSdkAppId(appId);
            req.setSignName(signName);
            req.setTemplateId(templateId);
            req.setTemplateParamSet(TemplateParamSet);
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            log.info("短信发送成功,返回值为:"+SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            e.printStackTrace();
        }
    }
}
